{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "同 Series 一样，DataFrame 自带行标签索引，默认为“隐式索引”即从 0 开始依次递增，行标签与 DataFrame 中的数据项一一对应。上述表格的行标签从 0 到 5，共记录了 5 条数据（图中将行标签省略）。当然你也可以用“显式索引”的方式来设置行标签。\n",
    "\n",
    "参数名称|说明\n",
    ":-|:-\n",
    "data|输入的数据，可以是 ndarray，series，list，dict，标量以及一个 DataFrame。\n",
    "index|行标签，如果没有传递 index 值，则默认行标签是 np.arange(n)，n 代表 data 的元素个数。\n",
    "columns|列标签，如果没有传递 columns 值，则默认列标签是 np.arange(n)。\n",
    "dtype|dtype表示每一列的数据类型。\n",
    "copy|默认为 False，表示复制数据 data。\n",
    "\n",
    "下面对 DataFrame 数据结构的特点做简单地总结，如下所示：\n",
    "- DataFrame 每一列的标签值允许使用不同的数据类型；\n",
    "- DataFrame 是表格型的数据结构，具有行和列；\n",
    "- DataFrame 中的每个数据值都可以被修改。\n",
    "- DataFrame 结构的行数、列数允许增加或者删除；\n",
    "- DataFrame 有两个方向的标签轴，分别是行标签和列标签；\n",
    "- DataFrame 可以对行和列执行算术运算。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>第一列</th>\n",
       "      <th>第二列</th>\n",
       "      <th>第三列</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>第一行</th>\n",
       "      <td>21</td>\n",
       "      <td>91</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>第二行</th>\n",
       "      <td>7</td>\n",
       "      <td>26</td>\n",
       "      <td>67</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>第三行</th>\n",
       "      <td>95</td>\n",
       "      <td>26</td>\n",
       "      <td>91</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     第一列  第二列  第三列\n",
       "第一行   21   91    6\n",
       "第二行    7   26   67\n",
       "第三行   95   26   91"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#创建 DataFrame\n",
    "data = np.random.rand(9).reshape(3,3)*100\n",
    "pd.DataFrame(data, index=[\"第一行\",\"第二行\",\"第三行\"],columns=[\"第一列\",\"第二列\",\"第三列\"],dtype=np.int8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     第一列  第二列  第三列\n",
      "第一行    1    2    3\n",
      "第二行    4    5    6\n",
      "第三行    7    8    9\n"
     ]
    }
   ],
   "source": [
    "# 使用列表创建\n",
    "data = [[1,2,3],[4,5,6],[7,8,9]]\n",
    "df = pd.DataFrame(data, index=[\"第一行\",\"第二行\",\"第三行\"],columns=[\"第一列\",\"第二列\",\"第三列\"],dtype=np.int8)\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "        one       two     three      four      five\n",
      "a  0.722994  0.293990  0.133031  0.473980  0.574293\n",
      "b  0.662710  0.425165  0.106359  0.341694  0.312058\n",
      "c  0.075847  0.838853  0.781176  0.715870  0.112189\n",
      "d  0.068151  0.027683  0.628439  0.666567  0.372179\n",
      "e  0.142519  0.539757  0.443668  0.086855  0.764167\n"
     ]
    }
   ],
   "source": [
    "# 使用series创建\n",
    "data = {\n",
    "    \"one\": pd.Series(np.random.rand(5),index=[\"a\",\"b\",\"c\",\"d\",\"e\"]),\n",
    "    \"two\": pd.Series(np.random.rand(5),index=[\"a\",\"b\",\"c\",\"d\",\"e\"]),\n",
    "    \"three\":pd.Series(np.random.rand(5),index=[\"a\",\"b\",\"c\",\"d\",\"e\"]),\n",
    "    \"four\": pd.Series(np.random.rand(5),index=[\"a\",\"b\",\"c\",\"d\",\"e\"]),\n",
    "    \"five\": pd.Series(np.random.rand(5),index=[\"a\",\"b\",\"c\",\"d\",\"e\"])\n",
    "}\n",
    "\n",
    "df = pd.DataFrame(data)\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    0.722994\n",
      "b    0.662710\n",
      "c    0.075847\n",
      "d    0.068151\n",
      "e    0.142519\n",
      "Name: one, dtype: float64\n",
      "        one       two     three      four      five  six\n",
      "a  0.722994  0.293990  0.133031  0.473980  0.574293  1.0\n",
      "b  0.662710  0.425165  0.106359  0.341694  0.312058  1.0\n",
      "c  0.075847  0.838853  0.781176  0.715870  0.112189  1.0\n",
      "d  0.068151  0.027683  0.628439  0.666567  0.372179  1.0\n",
      "e  0.142519  0.539757  0.443668  0.086855  0.764167  1.0\n",
      "        one       two     three      four      five\n",
      "a  0.722994  0.293990  0.133031  0.473980  0.574293\n",
      "b  0.662710  0.425165  0.106359  0.341694  0.312058\n",
      "c  0.075847  0.838853  0.781176  0.715870  0.112189\n",
      "d  0.068151  0.027683  0.628439  0.666567  0.372179\n",
      "e  0.142519  0.539757  0.443668  0.086855  0.764167\n"
     ]
    }
   ],
   "source": [
    "# 列索引操作\n",
    "print(df[\"one\"]) # 获取列\n",
    "\n",
    "df.insert(5,column=\"six\",value=[1.,1.,1.,1.,1.]) # 插入一列\n",
    "print(df)\n",
    "\n",
    "df.pop(\"six\")\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "one      0.722994\n",
      "two      0.293990\n",
      "three    0.133031\n",
      "four     0.473980\n",
      "five     0.574293\n",
      "Name: a, dtype: float64\n",
      "        one       two     three      four      five\n",
      "c  0.075847  0.838853  0.781176  0.715870  0.112189\n",
      "d  0.068151  0.027683  0.628439  0.666567  0.372179\n",
      "        one       two     three      four      five\n",
      "a  0.722994  0.293990  0.133031  0.473980  0.574293\n",
      "b  0.662710  0.425165  0.106359  0.341694  0.312058\n",
      "c  0.075847  0.838853  0.781176  0.715870  0.112189\n",
      "d  0.068151  0.027683  0.628439  0.666567  0.372179\n",
      "e  0.142519  0.539757  0.443668  0.086855  0.764167\n",
      "        one       two     three      four      five\n",
      "a  0.722994  0.293990  0.133031  0.473980  0.574293\n",
      "b  0.662710  0.425165  0.106359  0.341694  0.312058\n",
      "c  0.075847  0.838853  0.781176  0.715870  0.112189\n",
      "d  0.068151  0.027683  0.628439  0.666567  0.372179\n",
      "e  0.142519  0.539757  0.443668  0.086855  0.764167\n"
     ]
    }
   ],
   "source": [
    "# 行索引操作\n",
    "print(df.loc[\"a\"]) # 将行标签传递给 loc 函数，来选取数据\n",
    "\n",
    "print(df[2:4]) # 切片\n",
    "\n",
    "df.append(pd.DataFrame([[3.0,2.,1.,.5,.1]],columns=[\"one\",\"two\",\"three\",\"four\",\"five\"]))\n",
    "print(df)\n",
    "\n",
    "df.drop(\"a\")\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "DataFrame 的属性和方法：\n",
    "\n",
    "名称|属性&方法描述\n",
    ":-|:-\n",
    "T|行和列转置。\n",
    "axes|返回一个仅以行轴标签和列轴标签为成员的列表。\n",
    "dtypes|返回每列数据的数据类型。\n",
    "empty|DataFrame中没有数据或者任意坐标轴的长度为0，则返回True。\n",
    "ndim|轴的数量，也指数组的维数。\n",
    "shape|返回一个元组，表示了 DataFrame 维度。\n",
    "size|DataFrame中的元素数量。\n",
    "values|使用 numpy 数组表示 DataFrame 中的元素值。\n",
    "head()|返回前 n 行数据。\n",
    "tail()|返回后 n 行数据。\n",
    "shift()|将行或列移动指定的步幅长度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "        one       two     three      four      five\n",
      "a  0.722994  0.293990  0.133031  0.473980  0.574293\n",
      "b  0.662710  0.425165  0.106359  0.341694  0.312058\n",
      "c  0.075847  0.838853  0.781176  0.715870  0.112189\n",
      "d  0.068151  0.027683  0.628439  0.666567  0.372179\n",
      "e  0.142519  0.539757  0.443668  0.086855  0.764167\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>one</th>\n",
       "      <th>two</th>\n",
       "      <th>three</th>\n",
       "      <th>four</th>\n",
       "      <th>five</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>0.722994</td>\n",
       "      <td>0.293990</td>\n",
       "      <td>0.133031</td>\n",
       "      <td>0.473980</td>\n",
       "      <td>0.574293</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>0.662710</td>\n",
       "      <td>0.425165</td>\n",
       "      <td>0.106359</td>\n",
       "      <td>0.341694</td>\n",
       "      <td>0.312058</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>0.075847</td>\n",
       "      <td>0.838853</td>\n",
       "      <td>0.781176</td>\n",
       "      <td>0.715870</td>\n",
       "      <td>0.112189</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>e</th>\n",
       "      <td>0.068151</td>\n",
       "      <td>0.027683</td>\n",
       "      <td>0.628439</td>\n",
       "      <td>0.666567</td>\n",
       "      <td>0.372179</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        one       two     three      four      five\n",
       "a       NaN       NaN       NaN       NaN       NaN\n",
       "b  0.722994  0.293990  0.133031  0.473980  0.574293\n",
       "c  0.662710  0.425165  0.106359  0.341694  0.312058\n",
       "d  0.075847  0.838853  0.781176  0.715870  0.112189\n",
       "e  0.068151  0.027683  0.628439  0.666567  0.372179"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(df)\n",
    "df.shift(periods=1,axis=0) # 行列移动"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
